package com.fqcar.fqdf.adapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.fqcar.fqdf.R;
import com.fqcar.fqdf.entity.CityEntity;
import com.fqcar.fqdf.util.Constants;
import com.fqdf.framework.android.adapter.BaseRecycleAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhangxutong .
 * Date: 16/08/28
 */

public class CityListAdapter extends BaseRecycleAdapter<CityEntity, CityListAdapter.CityViewHolder> {

    public CityListAdapter(Activity mContext) {
        super(mContext);
    }

    public CityListAdapter(Activity mContext, List<CityEntity> mDatas) {
        super(mContext);
        setList(mDatas);
    }

    @Override
    public CityViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CityViewHolder(getLayoutInflater().inflate(R.layout.activity_item_city, parent, false));
    }

    @Override
    public void onBindViewHolder(final CityViewHolder holder, int position) {

        final CityEntity cityEntity = getItem(position);
        holder.cityName.setText(cityEntity.getName());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getListener() != null) {
                    getListener().onItemClick(v, holder.getAdapterPosition());
                }
            }
        });


        holder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putParcelable(Constants.Tag.CITY_ENTITY, cityEntity);
//                bundle.putSerializable(Constants.Tag.CITY_ENTITY,cityEntity);
                intent.putExtras(bundle);

                Activity activity = (Activity) getContext();
                activity.setResult(Activity.RESULT_OK, intent);
                activity.finish();
            }
        });
    }

    public static class CityViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.city_name)
        TextView cityName;
        @BindView(R.id.content)
        LinearLayout content;

        CityViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
